//
//  ESWeatherCityListViewController.m
//  EasyStyle
//
//  Created by FK on 16/5/15.
//  Copyright © 2016年 FK. All rights reserved.
//

#import "ESWeatherCityListViewController.h"
#import "ESLocationSupport.h"

@interface ESWeatherCityListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *cityList;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSString *locateResult;

@end

BOOL isLocationSupport = NO;

@implementation ESWeatherCityListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(cityLocationUpdate:) name:LocationSupportUpdateWeatherNotification object:nil];
    
    [self setupRootView:self.view.frame];
    [self loadData];
    
    [[ESLocationSupport sharedInstance]startUpdatingLocation];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    _cityList = [[NSUserDefaults standardUserDefaults]objectForKey:LocateCityList];
}

#pragma mark -- 布局相关
- (void)setupRootView:(CGRect)parentFrame
{
    
    self.navigationController.navigationBarHidden = YES;
    [self addCustomNavigationBarWithStyle:DefaultStyle customColor:[UIColor colorWithHex:@"48dbee"]];
    WS(weakSelf);
    [self.sgCustomNavigationBar showBackButtonWithActionBlock:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.sgCustomNavigationBar.height, self.view.width, self.view.height-self.sgCustomNavigationBar.height) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorInset = UIEdgeInsetsMake(0,13,0,13);
    [self.view addSubview:_tableView];

}

- (void)cityLocationUpdate:(NSNotification *)aNotification
{
    if ([aNotification.name isEqualToString:LocationSupportUpdateWeatherNotification]) {
        isLocationSupport = YES;
        _locateResult = [[ESDataController shareInstance]getUserCityName];
        [_tableView reloadData];
    }
}


#pragma mark -- UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (isLocationSupport) {
        return 2;
    }
    
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (isLocationSupport) {
        if (section == 0) {
            return @"定位";
        } else if (section == 1) {
            return @"全部城市";
        }
    }
    return @"全部城市";
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (isLocationSupport) {
        if (section == 0) {
            return 1;
        } else if (section ==1) {
            return _cityList.count;
        }
        
    }
    return _cityList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WeatherCityListCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"WeatherCityListCell"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
    }
    
    if (isLocationSupport) {
        if (indexPath.section == 0) {
            cell.textLabel.text = _locateResult;
        } else if (indexPath.section == 1) {
            cell.textLabel.text = [_cityList objectAtIndex:indexPath.row];
        }
        
    } else {
        cell.textLabel.text = [_cityList objectAtIndex:indexPath.row];
    }
    return cell;
}


#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    [[ESDataController shareInstance]saveUserCityName:cell.textLabel.text];
    [[NSNotificationCenter defaultCenter]postNotificationName:LocationSupportUpdateWeatherNotification object:cell.textLabel.text];
    
    [self.navigationController popViewControllerAnimated:YES];
}


@end
